import Vue from 'vue'
import Vuex from 'vuex'
import router from '../router'
// 请求
import { siteLogin } from '@/api/user'
import { setToken } from '@/utils/auth'
Vue.use(Vuex)

const states = new Vuex.Store({
  state: {
    Token: ''
  },
  mutations: {
    Set_Token: (state, Token) => {
      state.Token = Token
    }
  },
  actions: {
    login ({commit}, params) {
      let { username, password } = params
      return new Promise((resolve, reject) => {
        siteLogin({ account: username, pwd: password }).then(res => {
          let { loginToken } = res
          commit('Set_Token', loginToken)
          setToken(loginToken)
          router.push('/layout')
          resolve()
        }).catch(err => {
          reject(err)
        })
      })
    }
  },
  getters: {
    token: state => state.Token
  }
})

export default states
